/**
 * Created by on 2017/6/14.
 */
import React from "react";
import Relay from "react-relay";
import {hashHistory} from 'react-router';
import Util from "../../../../../util/Util";
import { Table, Icon, message } from 'antd';
import { Button } from 'fly';
import InfoOrderMutation from "../../../mutation/InfoOrderMutation";
import InfoEnquiryOrderMutation from "../../../mutation/InfoEnquiryOrderMutation";
import OrderDetailIndexTable from './OrderDetailIndexTable';
import "./css/orderDetailStyle.css"
/*信息列表*/
export default class Orderdetail_cloth extends React.Component{
    constructor(props){
        super(props);
        this.state={
            num:0,
            arr:[],
            order:[],
            width:0,
            deliverRequests: [],
            isLoading: true
        };
        this.orderpartA = "";
        this.enterpriseId = sessionStorage.getItem('enterpriseId');
        this.serviceType = sessionStorage.getItem('serviceType');
        this.arr2=[]
        this.money={
            total:87887,pay:6786
        }
    }

    componentWillMount(){
        if(this.props.location.state.enquiryId !='' && this.props.location.state.enquiryId != undefined) {
            this.EnquiryOrderdetail();
        } else {
            this.Orderdetail();
        }
    }

    EnquiryOrderdetail() {
        // this.state.width=this.money.pay/this.money.total*460
        this.setState({
            arr:this.arr
        });
        /*infoorder接口*/
        let oid = this.props.location.state.oid
        Relay.Store.commitUpdate(
            new InfoEnquiryOrderMutation({
                orderId: oid,
                enId: this.enterpriseId,
                enquiryId: this.props.location.state.enquiryId
            }),
            {
                onFailure: () => {
                    message.info('请求失败')
                    this.setState({
                        isLoading: false
                    })
                },
                onSuccess: (response) => {
                    let errorCode = response.infoEnquiryOrder.infoEnquiryOrder.errorCode;
                    if(errorCode=='0'){
                        let order = response.infoEnquiryOrder.infoEnquiryOrder.order;
                        let deliverRequests = response.infoEnquiryOrder.infoEnquiryOrder.order.deliverRequests;
                        let orderGoodss = response.infoEnquiryOrder.infoEnquiryOrder.order.orderGoodss
                        orderGoodss.push({gid: 'key1'},{gid: 'key2'})
                        if (orderGoodss) {
                            orderGoodss.map((o,m) => {
                                if (o.quantity && o.price) {
                                    o.tableOrderMoney = (o.quantity*o.price).toFixed(2)
                                }
                                return o
                            })
                        }
                        if (deliverRequests) {
                            deliverRequests.map((v, i)=> {
                                v.deliverDate = Util.dateFormat(v.deliverDate)   //时间戳转换
                                return v
                            })
                        }
                        this.orderpartA = order.partA;
                        this.setState({
                            order: order,
                            deliverRequests: deliverRequests,
                            isLoading: false
                        },()=>{
                            if(this.refs.ordercinfo){
                                this.refs.ordercinfo.setState({
                                    supplyContractsData : order.supplyContracts
                                });
                            }
                        });
                    } else {
                        message.info('请求失败')
                    }
                }
            }
        )
    }

    Orderdetail() {
      this.setState({
          arr:this.arr
      });
      /*infoorder接口*/
      let oid = this.props.location.state.oid
      Relay.Store.commitUpdate(
          new InfoOrderMutation({
              orderId: oid,
              // enId: this.enterpriseId,
              enId: "0",
          }),
          {
              onFailure: () => {
                  message.info('请求失败')
                  this.setState({
                    isLoading: false
                  })
              },
              onSuccess: (response) => {
                  let errorCode = response.infoOrder.infoOrder.errorCode;
                  if(errorCode=='0'){
                    let order = response.infoOrder.infoOrder.order;
                    let deliverRequests = response.infoOrder.infoOrder.order.deliverRequests;
                    let orderGoodss = response.infoOrder.infoOrder.order.orderGoodss
                    orderGoodss.push({gid: 'key1'},{gid: 'key2'})
                    if (orderGoodss) {
                      orderGoodss.map((o,m) => {
                        if (o.quantity && o.price) {
                          o.tableOrderMoney = (o.quantity*o.price).toFixed(2)
                        }
                        return o
                      })
                    }
                    if (deliverRequests) {
                      deliverRequests.map((v, i)=> {
                        v.deliverDate = Util.dateFormat(v.deliverDate)   //时间戳转换
                        return v
                      })
                    }
                    this.orderpartA = order.partA;
                    this.setState({
                        order: order,
                        deliverRequests: deliverRequests,
                        isLoading: false
                    },()=>{
                      if(this.refs.ordercinfo){
                        this.refs.ordercinfo.setState({
                          supplyContractsData : order.supplyContracts
                        });
                      }
                    });
                    // this.props.orderDetail()
                  } else {
                    message.info('请求失败')
                  }
              }
          }
      )
    }

    backClick() {
      let record = {}
      record.enquiryId = this.props.location.state.newEnquiryId;
      record.oid = this.state.order.oid
      hashHistory.push({
        pathname:'/OrderIndex/',
        state: {defaultActiveKey: '8', record: record}
      })
    }

    /*合同附件*/
    apply(){
        let contractUrl = [];
        let contractUrlSum = [];
        this.state.order.contractUrl1 != null ? contractUrl.push(this.state.order.contractUrl1) : null
        this.state.order.contractUrl2 != null ? contractUrl.push(this.state.order.contractUrl2) : null
        this.state.order.contractUrl3 != null ? contractUrl.push(this.state.order.contractUrl3) : null
        this.state.order.contractUrl4 != null ? contractUrl.push(this.state.order.contractUrl4) : null
        this.state.order.contractUrl5 != null ? contractUrl.push(this.state.order.contractUrl5) : null
        this.state.order.contractUrl1 != null && this.state.order.contractUrl1 != '' ? contractUrlSum.push(Util.signedContractStr(this.state.order.contractUrl1)) : null
        this.state.order.contractUrl2 != null && this.state.order.contractUrl2 != '' ? contractUrlSum.push(Util.signedContractStr(this.state.order.contractUrl2)) : null
        this.state.order.contractUrl3 != null && this.state.order.contractUrl3 != '' ? contractUrlSum.push(Util.signedContractStr(this.state.order.contractUrl3)) : null
        this.state.order.contractUrl4 != null && this.state.order.contractUrl4 != '' ? contractUrlSum.push(Util.signedContractStr(this.state.order.contractUrl4)) : null
        this.state.order.contractUrl5 != null && this.state.order.contractUrl5 != '' ? contractUrlSum.push(Util.signedContractStr(this.state.order.contractUrl5)) : null
        return contractUrlSum.length ? contractUrlSum.map((v,i)=>{
            return (<p className="main_order_detail_box_download"><span>{v}</span><a onClick={this.downloadtext.bind(this,contractUrl,i)}>下载</a></p>)
        }): '无'
    }
    downloadtext(contractUrl, i) {
        const h = contractUrl[i], a = document.createElement("a");
        a.href = h;
        a.setAttribute("download","");
        a.click();
    }

    render() {
      const columns = [
        {
          title: '交货时间',
          dataIndex: 'deliverDate',
          key: 'deliverDate',
        }, {
          title: '数量',
          dataIndex: 'amount',
          key: 'amount',
        }, {
          title: '备注',
          dataIndex: 'remark',
          key: 'remark',
        }
      ];
      const { partA, partB, status } = this.state.order;
      const flag = (partA != this.enterpriseId && status >= 3 && (partB != this.enterpriseId))   // 已失效
      return (
          <div className="main_order_detail_box">
            {
              this.state.isLoading ?
                <div style={{background:'#fff',position: 'absolute',height: '100%',zIndex: '3'}}><Icon style={{left: '55%',position: 'fixed',top: '45%',fontSize: '24px'}} type="loading" /></div>
              : (
                <div className='main_order_detail_box_info' style={{padding:'0 20px'}}>
                    <p className="main_order_detail_box_backbtn"><Button onClick={this.backClick.bind(this)}>返回</Button></p>
                    <div className="main_order_detail_box_info_top">
                      <ul>
                        <li>甲方：{this.state.order.partAName == 'null' ? '无' : this.state.order.partAName}</li>
                        <li>地址：{this.state.order.partAAddress == 'null' ? '无' : this.state.order.partAAddress}</li>
                        <li>联系人：{this.state.order.partAUserName == 'null' ? '无' : this.state.order.partAUserName}  {this.state.order.partAPhone == 'null' ? '无' : this.state.order.partAPhone}</li>
                      </ul>
                      {
                        flag ? (
                          <p className="main_order_detail_box_lapsed"></p>
                        ) : (
                          <ul>
                            <li>乙方：{this.state.order.partBName == 'null' ? '无' : this.state.order.partBName}</li>
                            <li>地址：{this.state.order.partBAddress == 'null' ? '无' : this.state.order.partBAddress}</li>
                            <li>联系人：{this.state.order.partBUserName == 'null' ? '无' : this.state.order.partBUserName}  {this.state.order.partBPhone == 'null' ? '无' : this.state.order.partBPhone}</li>
                          </ul>
                        )
                      }
                    </div>
                    <div>
                        <OrderDetailIndexTable orderType={this.state.order.orderType} order={this.state.order} />
                        <br/>
                         { /*交货要求*/  }
                        <Table
                          dataSource={this.state.deliverRequests}
                          columns={columns}
                          className="order_table order_detail_table requireTable"
                          // bordered
                          rowKey={record => record.did}
                          pagination={false}
                        />
                    </div>
                    <div>
                      <ul className="main_order_detail_box_content">
                          <li>
                              <span className="main_order_detail_box_content_title">质量要求: </span>
                              <span className="main_order_detail_box_content_require">{this.state.order.orderOthers && this.state.order.orderOthers.qualityAsk ? this.state.order.orderOthers.qualityAsk : '无'}</span>
                          </li>
                          <li>
                              <span className="main_order_detail_box_content_title">包装要求:</span>
                              <span className="main_order_detail_box_content_require">{this.state.order.orderOthers && this.state.order.orderOthers.packageAsk ? this.state.order.orderOthers.packageAsk : '无'}</span>
                          </li>
                          <li>
                              <span className="main_order_detail_box_content_title">物流要求:</span>
                              <span className="main_order_detail_box_content_require">{this.state.order.orderOthers && this.state.order.orderOthers.logisticsAsk ? this.state.order.orderOthers.logisticsAsk : '无'}</span>
                          </li>
                          <li>
                              <span className="main_order_detail_box_content_title">付款要求:</span>
                              <span className="main_order_detail_box_content_require">{this.state.order.orderOthers && this.state.order.orderOthers.payMode ? this.state.order.orderOthers.payMode : '无'}</span>
                          </li>
                          <li>
                              <span className="main_order_detail_box_content_title">违约责任:</span>
                              <span className="main_order_detail_box_content_require">{this.state.order.orderOthers && this.state.order.orderOthers.dutyBreak ? this.state.order.orderOthers.dutyBreak : '无'}</span>
                          </li>
                          <li>
                              <span className="main_order_detail_box_content_title">验收要求:</span>
                              <span className="main_order_detail_box_content_require">{this.state.order.orderOthers && this.state.order.orderOthers.checkAsk ? this.state.order.orderOthers.checkAsk : '无'}</span>
                          </li>
                          <li>
                              <span className="main_order_detail_box_content_title">备注说明:</span>
                              <span className="main_order_detail_box_content_require">{this.state.order.rmark ||　'无'}</span>
                          </li>
                          <li>
                              <span className="main_order_detail_box_content_title">附件信息:</span>
                              <span>
                                {
                                  this.apply()
                                }
                              </span>
                          </li>
                      </ul>
                    </div>
                    <div className="main_order_detail_box_isAcceptInfo">
                        <p>发送给的乙方</p>
                        <ul>
                          {
                            this.state.order.orderOperates ? this.state.order.orderOperates.map((v,i)=>{
                                return <li>{v.toUserName}&nbsp;&nbsp;&nbsp;&nbsp;
                                  {
                                    v.status >= '2' ? '已接单': '未接单'
                                  }
                                </li>
                            }) : null
                          }
                            <li></li>
                        </ul>
                    </div>
                </div>
              )
            }
          </div>
      )
    }
}
